#ifndef MACHINE_H
#define	MACHINE_H

/*******************************************************************************
 **  Name: machine.h                                                          **
 **  Description:  
 **  Author: bjames                                                           **
 **                                                                           **
 **  CDDL: Open Source Initiative (OSI) Approved License                      **
 **                                                                           **
 **  Copyright (c) 2009-2010 Barry "Gian" James  <bjames@munixos.net>         **
 **  All rights reserved.                                                     **
 **                                                                           **
 **  The contents of this file are subject to the terms of the CDDL:          **
 **  Common Development and Distribution License (the "License").             **
 **  You may not use this file except in compliance with the License.         **
 **                                                                           **
 **  You can obtain a copy of the license at $PROJECT_ROOT/LICENSE            **
 **  or http://www.opensolaris.org/os/licensing.  This code is Open Source    **
 **  and you are free to use it within the confines of the license, even      **
 **  for your own closed-source commercial projects, as long as you follow    **
 **  the terms set forth in the CDDL.                                         **
 **                                                                           **
 **  When distributing Covered Code, include this CDDL HEADER in each         **
 **  file and include the License file at $PROJECT_ROOT/LICENSE.              **
 **  If applicable, add the following below this CDDL HEADER, with the        **
 **  fields enclosed by brackets "[]" replaced with your own identifying      **
 **  information: Portions Copyright [yyyy] [name of copyright owner]         **
 **                                                                           **
 ******************************************************************************/
// Initialized on September 28, 2010, 2:11 PM
#define _MUNIX_machine_h_ID_ "$Id$"

#include "common.h"
#include "opcodes.h"


#define	ZBIT	0
#define PBIT	1
#define CBIT	2


/// class MachineEmulator
/// An S-Machine implementation.  An S-Machine has four registers and
/// two stores.
class	MachineEmulator
{
public:
    MachineEmulator();

    void	ListCode(u32);
    void	Interpret(u32,u16);
    OpCode	Opcode(char *);
    void	Emulate(u16,u32,u16, FILE *, FILE *, bool);

    void	SetZBit() { cpu0.flags |= (1<<ZBIT); }
    void	SetPBit() { cpu0.flags |= (1<<PBIT); }
    void    SetCBit() { cpu0.flags |= (1<<CBIT); }
    int     GetZBit() const { return (cpu0.flags & ZBIT); }
    int     GetPBit() const { return (cpu0.flags & PBIT); }
    int 	GetCBit() const { return (cpu0.flags & CBIT); }
    void	ClrZBit() { cpu0.flags &= ~(1<<ZBIT); }
    void	ClrPBit() { cpu0.flags &= ~(1<<PBIT); }
    void	ClrCBit() { cpu0.flags &= ~(1<<CBIT); }
    int *      Stack() { return mem; }

private:
    void	_trace(FILE *, u16);
    void	_postmortem(FILE *, byte);
    void    _stackdump(u32 initsp, FILE *results, u32 pcnow);
    bool    _inbounds(int);
//    byte	_index() { return ((mem[cpu0.pc] + cpu0.x) % 256); }

    u32         stackmax, stackmin;
    struct	processor
    {
        u16	sp;	// stack pointer
        u16	bp;	// base pointer
        OpCode	ir;	// instruction register
        u16	pc;	// program counter
        u16     mp;     // mark stack pointer
        u32	flags;
    };
    processor	cpu0;
    status	ps;
    static const char *mnemonics[]; // opcode mnemonics
    int	mem[STACK_MEM_MAX]; // virtual machine memory
    static const char *directives[];
//    struct instruction {
//        OpCode  op;
//        int     arg;
//    }     code[CODE_MEM_MAX];
//    int   stack[STACK_MEM_MAX];
};



#endif	/* MACHINE_H */

